//
//  NSString+Category.m
//  FaceLive
//
//  Created by zhangyi on 2017/6/20.
//  Copyright © 2017年 mty. All rights reserved.
//

#import "NSString+Category.h"

@implementation NSString (Category)

//判断内容是否全部为空格  yes 全部为空格  no 不是
- (BOOL)isEmpty{
    
    if (!self) {
        return YES;
    } else {
        NSCharacterSet *set = [NSCharacterSet whitespaceAndNewlineCharacterSet];
        NSString *trimedString = [self stringByTrimmingCharactersInSet:set];
        if ([trimedString length] == 0) {
            return YES;
        } else {
            return NO;
        }
    }
}

- (CGSize)sizeWithFont:(UIFont *)font maxSize:(CGSize)maxSize
{
    NSDictionary *attrs = @{NSFontAttributeName : font};
    return [self boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attrs context:nil].size;
}


- (NSArray <NSTextCheckingResult *> *)machesWithPattern:(NSString *)pattern
{
    NSError *error = nil;
    NSRegularExpression *expression = [NSRegularExpression regularExpressionWithPattern:pattern options:0 error:&error];
    if (error)
    {
        LOG(@"正则表达式创建失败");
        return nil;
    }
    return [expression matchesInString:self options:0 range:NSMakeRange(0, self.length)];
}
//对象转json
+(NSString *)objectToJson:(id)object{
    NSError *parseError = nil;
    NSData  *jsonData = [NSJSONSerialization dataWithJSONObject:object options:NSJSONWritingPrettyPrinted error:&parseError];
    return [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
}


/**
 手机号校验
 
 @return yes/no
 */
- (BOOL)isMobilePhone{
    
    // 去掉前后空格
    NSString *trimmedString = [self stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

    if (trimmedString.length != 11) {
        return NO;
    }
    if ([self deptNumInputShouldNumber:trimmedString]) {
        return  YES;
    }
//    if ([trimmedString hasPrefix:@"1"] && [self deptNumInputShouldNumber:trimmedString]) {
//        return  YES;
//    }
    return NO;
}

- (BOOL) deptNumInputShouldNumber:(NSString *)str
{
    if (str.length == 0) {
        return NO;
    }
    NSString *regex = @"[0-9]*";
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex];
    if ([pred evaluateWithObject:str]) {
        return YES;
    }
    return NO;
}
@end
